package com.rgs.se.data.array;

/**
 * @author Ren Gaoshuai
 * @ClassName Array
 * @date 2022-04-03 16:41
 **/
public class Array<E> {
    private E[] data;
    private int size;

    public Array() {
        this(10);
    }

    public Array(int capacity) {
        this.data = (E[]) new Object[capacity];
        this.size = 0;
    }

    public int getSize() {
        return size;
    }

    public int getCapacity() {
        return data.length;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public E getLast() {
        return index(size - 1);
    }

    /**
     * 默认添加
     *
     * @param e
     */
    public void add(E e) {
        this.addLast(e);
    }

    /**
     * 末尾添加
     *
     * @param e
     */
    public void addLast(E e) {
        if (size == data.length) {
            //扩容
            grow(data.length << 1);
        }
        data[size++] = e;
    }

    public void grow(int newCapacity) {
        E[] newArr = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newArr[i] = data[i];
        }
        data = newArr;
    }

    /**
     * 头部添加
     *
     * @param e
     */
    public void addFirst(E e) {
        addIndex(0, e);
    }

    /**
     * 指定位置添加
     *
     * @param index
     * @param e
     */
    public void addIndex(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("索引违法");
        } else if (size == data.length) {
            //扩容
            grow(data.length << 1);
        } else {
            for (int i = size - 1; i >= index; i--) {
                //向后移动
                data[i + 1] = data[1];
            }
            data[index] = e;
            size++;
        }
    }

    public E index(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("索引违法");
        } else {
            return data[index];
        }
    }

    public E delete(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("索引违法");
        }
        if (size < data.length >> 1 && size > 10) {
            //扩容
            grow(data.length >> 1);
        }
        E old = data[index];

        for (int i = index; i < size; i++) {
            data[i] = data[i + 1];
        }
        size--;
        return old;

    }

    public E removeLast() {
        if (size < data.length >> 1 && size > 10) {
            //缩小容量
            grow(data.length >> 1);
        }
        E old = data[size - 1];
        data[size - 1] = null;
        size--;
        return old;

    }

    public E update(int index, E e) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("索引违法");
        } else {
            E old = data[index];
            data[index] = e;
            return old;
        }
    }

    public boolean contains(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i] == e) return true;
        }
        return false;
    }
}
